﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Common;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;

namespace JM.JCY.SharePointComponent.Event.SearchEventReceiver
{
    public partial class SearchEventReceiverUserControl : UserControl
    {
        public int _userId
        {
            set { ViewState["_userId"] = value; }
            get { return (int)ViewState["_userId"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ApplicationContext.Current.GetKeyValue("userId") != null)
            {
                _userId = Int32.Parse(ApplicationContext.Current.GetKeyValue("userId").ToString());
            }

            if (!IsPostBack)
            {
                BindControls();

                string subject = this.txtSubject.Text;

                DateTime? start = null;
                DateTime? end = null;

                if (!dtEffDateFrom.IsDateEmpty)
                    start = this.dtEffDateFrom.SelectedDate;

                if (!dtEffDateTo.IsDateEmpty)
                    end = this.dtEffDateTo.SelectedDate;

                 using (EventProxy proxy = new EventProxy())
                {
                    EventDataset.T_Event_S_ByReceiverDataTable dt = proxy.GetEventByReceiver(subject, _userId, start, end, JCYConstant.Status.Pending);
                    this.gvBC.DataSource = dt;
                    this.gvBC.DataBind();
                }
            }
        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            string subject = this.txtSubject.Text;

            DateTime? start = null;
            DateTime? end = null;

            if (!dtEffDateFrom.IsDateEmpty)
                start = this.dtEffDateFrom.SelectedDate;

            if (!dtEffDateTo.IsDateEmpty)
                end = this.dtEffDateTo.SelectedDate;

            string status = string.Empty;
            // status
            if (this.ddlStatus.SelectedIndex == 1)
            {
                status = JCYConstant.Status.Finish;
            }
            else if (this.ddlStatus.SelectedIndex == 2)
            {
                status = JCYConstant.Status.Pending;
            }

            using (EventProxy proxy = new EventProxy())
            {
                EventDataset.T_Event_S_ByReceiverDataTable dt = proxy.GetEventByReceiver(subject, _userId, start, end, status);
                this.gvBC.DataSource = dt;
                this.gvBC.DataBind();
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("SearchEventReceiver.aspx");
        }

        protected void gvBC_DataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblStatus = (Label)e.Row.Cells[3].FindControl("lblStatus");
                if (lblStatus.Text.Equals(JCYConstant.Status.Finish))
                    lblStatus.Text = "已完成";
                else
                    lblStatus.Text = "未完成";

                //load date format
                Label lblEffDate = (Label)e.Row.Cells[2].FindControl("lblEffDate");

                string dateString = lblEffDate.Text;
                DateTime dateTime = DateTime.Parse(dateString);
                lblEffDate.Text = dateTime.ToString(JCYConstant.Date_Format_CN);
            }
        }

        private void BindControls()
        {
            this.ddlStatus.Items.Insert(0, new ListItem(JCYConstant.DropdownlistEmptyText, ""));
            this.ddlStatus.Items.Insert(1, "已完成");
            this.ddlStatus.Items.Insert(2, "未完成");

        }
    }
}
